/**
 * 
 */
package fr.ece.ing4.si.mc.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.Border;

import fr.ece.ing4.si.mc.MonteCarlo.MonteCarloControllerView;

/**
 * @author Cesar Berezowski, Gaspard Peyrot
 */
public class ResultWindow extends JFrame implements MonteCarloControllerView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Pointer on the main window 
	 */
	private MainWindow mainWindow;
	/**
	 * Pointer on this
	 */
	private ResultWindow r = this;
	/**
	 * Progress bar displaying the computation progression
	 */
	private JProgressBar progressBar;
	/**
	 * Label to display the calculated price
	 */
	private JLabel resultValue;
	/**
	 * Label to display the computation price
	 */
	private JLabel timeValue;
	/**
	 * Button to go back to the main window
	 */
	private JButton newTest;
	/**
	 * Button to quit
	 */
	private JButton quit;
	
	/**
	 * Window's constructor
	 */
	public ResultWindow(MainWindow mainWindow) {
		super();
		this.mainWindow = mainWindow;
		this.build();
	}
	
	/**
	 * Building window
	 */
	private void build(){
		setTitle("Monte Carlo results");
		setSize(300, 200);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setContentPane(buildContentPanel());
	}
	
	/**
	 * Creation of window's panel and components
	 * @return JPanel
	 */
	private JPanel buildContentPanel(){
		
		// Variable declaration and initialisation
		JPanel p = new JPanel();
		GridBagConstraints c = new GridBagConstraints();
		
		// Setting the layout
		p.setLayout(new GridBagLayout());
		
		// JProgressBar progressBar : creation and adding to the layout
		progressBar = new JProgressBar();
		progressBar.setValue(0);
	    progressBar.setStringPainted(true);
	    Border border = BorderFactory.createTitledBorder("Computing...");
	    progressBar.setBorder(border);
	    c.gridx = 0;
	    c.gridy = 0;
	    c.gridwidth = 2;
	    p.add(progressBar, c);
	    
	    // JLabel result : creation and adding to the layout
	    JLabel result = new JLabel("Result : ");
		c.anchor = GridBagConstraints.EAST;
	    c.gridwidth = 1;
	    c.gridx = 0;
	    c.gridy = 1;
	    p.add(result, c);
	    
		// JLabel resultValue : creation and adding to the layout
	    resultValue = new JLabel("X");
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 1;
		c.gridy = 1;
		p.add(resultValue, c);
		
		// JLabel time : creation and adding to the layout
		JLabel time = new JLabel("Computing time : ");
		c.anchor = GridBagConstraints.EAST;
	    c.gridx = 0;
	    c.gridy = 2;
	    p.add(time, c);
	    
	    // JLabel timeValue : creation and adding to the layout
	    timeValue = new JLabel("X");
	    c.anchor = GridBagConstraints.WEST;
		c.gridx = 1;
		c.gridy = 2;
		p.add(timeValue, c);
		
		// JButton newTest : creation and adding to the layout plus adding listener
		// Used to go back to the main window
		newTest = new JButton("New test");
		setNewTestListener();
		c.anchor = GridBagConstraints.EAST;
	    c.gridx = 0;
	    c.gridy = 3;
	    p.add(newTest, c);
	    
		// JButton quit : creation and adding to the layout plus adding listener
		// Used to go quit the application
	    quit = new JButton("Quit");
	    setQuitListener();
	    c.anchor = GridBagConstraints.WEST;
	    c.gridx = 1;
	    c.gridy = 3;
	    p.add(quit, c);
	    
	    
		return p;
	}
	
	/**
	 * Adds the listener on quit button
	 */
	private void setQuitListener(){
		quit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				mainWindow.dispose();
				r.dispose();
				System.exit(0);
				
			}  });
	}
	
	/**
	 * Adds the listener on newTest button
	 */
	private void setNewTestListener(){
		newTest.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				mainWindow.newTest();
			}
			
		});
	}

	/**
	 * Updates the progress bar
	 */
	@Override
	public void updateProgress(int progress) {
		
		progressBar.setValue(progressBar.getValue() + progress);
	}
	
	/**
	 * Displays the final price and computation price
	 */
	@Override
	public void setValueAndTime(double value, long execTime) {
		
		resultValue.setText(Double.toString(value));
		timeValue.setText(Integer.toString((int)execTime) + "ms");
		progressBar.setValue(100);
		progressBar.setString("Done");
	}
}
